## transfer bed 6 column format to wig format, the fourth column which contain score would be used as wig file's score.
## author: Yaping Liu  lyping1986@gmail.com


my $input_file_name = $ARGV[0];

my $use_age = "USAGE: perl bed2wig.pl input_file_name ";
if($ARGV[0] eq ""){
	print "$use_age\n";
	exit(1);
}
my $file_name_output = $input_file_name;
$file_name_output =~ s/\.bed//;
$file_name_output = $file_name_output.".wig";
open(OUT,">$file_name_output") or die;



my $head_line = "track type=wiggle_0 name=".$file_name_output." description=\"$file_name_output\"";
print OUT "$head_line\n";


my $chr_flag = "";
open(FH,"<$input_file_name") or die;
while(<FH>){
	$line=$_;
	chomp($line);
	next if $line =~ /^track/;
	my @splitin = split "\t", $line;
	if($splitin[0] ne $chr_flag){
			$chr_flag = $splitin[0];
			my $chr_head_line = "variableStep chrom=$chr_flag";
			print OUT "$chr_head_line\n";
	}
	for(my $cor=$splitin[1]+1; $cor<= $splitin[2]; $cor++){
		my $methy = $splitin[4];
		#my $cor = $splitin[2];
		my $out_line = "$cor\t$methy";
		print OUT "$out_line\n";
	}
		
}

close(FH);
close(OUT);
print "finished!\n";

